<template>
  <div class="single-cell">
    <div class='title'>
      {{index}}.{{title}}
    </div>
    <div class='content'>
      <mt-checklist v-model="current" :options="optionList" :max="maxValue"></mt-checklist>
      <mt-field placeholder="请输入内容" v-model="other" class="input" v-if="hasOther"></mt-field>
    </div>
  </div>
</template>

<script>
import {  Field } from 'mint-ui';
import Checklist from './Checklist.vue';
export default {
  name: 'MultiSelect',
  components: {
    'mt-checklist': Checklist ,
    'mt-field': Field
  },
  props: {
    index: Number,
    title: String,
    options: String,
    max: String,
    value: String
  },
  data() {
    return {
      current: [],
      other: '',
      hasOther: false
    }
  },
  computed: {
    optionList: function() {
      if (this.options && this.options.length > 0) {
        let array = JSON.parse(this.options)
        var opp = []
        var has = false
        for (var i = 0; i < array.length; i++) {
          if(array[i] == '==!>') {
            has = true
          }
          else {
            opp.push(array[i])
          }
        }
        if (has) {
          opp.push('其他')
          this.hasOther = true
        }
        return opp
      }
      else {
        return []
      }
    },
    maxValue: function() {
      return parseInt(this.max)
    }
  },
  watch: {
    current: function(val) {
      var has = false
      var opp = []
      for (var i = 0; i < val.length; i++) {
        if (val[i] == '其他') {
          if (this.other.length == 0) {
            has = true
          }
          else {
            opp.push(this.other)
          }
        }
        else {
          opp.push(val[i])
        }
      }
      if (has) {opp = []}
      this.$emit('input', opp.join('/'))
    },
    other: function(val) {
      var has = false
      var opp = []
      for (var i = 0; i < this.current.length; i++) {
        if (this.current[i] == '其他') {
          if (val.length == 0) {
            has = true
          }
          else {
            opp.push(val)
          }
        }
        else {
          opp.push(this.current[i])
        }
      }
      if (has) {opp = []}
      this.$emit('input', opp.join('/'))
    }
  }
}
</script>

<style lang="scss">
.mint-checkbox-input {
  vertical-align: top
}
.mint-checkbox-label {
  vertical-align: bottom
}
.single-cell {
  padding: 50px 40px 0;
}
.single-cell > .title {
  font-size: 34px
}
.single-cell > .content {
  margin-top: 20px;
  position: relative;

  .mint-checklist {
    .mint-cell-title {
      padding: 10px 0;
    }
  }

  .mint-field {
    input[type="text"] {
      background-color: #f6f6f6;
      border: none;
      border-radius: 6px;
      height: 60px;
      padding:  0 20px;
      font-size: 26px;
      width: calc(100% - 90px);
      margin-left: 50px;
    }
  }
}

</style>
